import { request } from "@/src/service";
import Taro from "@tarojs/taro";

const customer_contract_get = (data) =>
  request({
    api: "customer.contract.get",
    data,
    flag: "customer-wechat",
  });

const createContract = async (contract_id: number, cb) => {
  Taro.showLoading();
  const { result, status } = await customer_contract_get({ contract_id });
  Taro.hideLoading();
  if (status === "ok") {
    if (typeof cb === "function") {
      if (result.contract_info.status === "signed") {
        Taro.navigateTo({
          url: `/pages/contratPreview/index?params=${JSON.stringify({
            ...result.contract_info,
            img_url: result.contract_info.contract_img_list,
          })}`,
        });
        return;
      }
      cb({
        ...result.contract_info,
        img_url: result.contract_info.contract_img_list,
      });
    }
  }
};

export default createContract;
